/*
 * Created on May 22, 2004
 *
 * Vectrics Inc.  GNU Public License.
 */
package org.vectrics.testing;

import java.util.Collection;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.common.db.CodedObject;
import org.vectrics.common.db.RecordObject;

import junit.framework.AssertionFailedError;

/**
 * @author mmoore
 */
public class TestUtils {
    private static final Logger log = Logger.getLogger(TestUtils.class);
	/**
	 * 
	 */
	public TestUtils() {
		super();
		// TODO Auto-generated constructor stub
	}

	
	public static void assertRecordObjectInList(RecordObject desiredObject, 
			Collection objectList) {
		assertRecordObjectInList("Assertion failed, record should be in list", desiredObject, objectList);
	}
	
	public static void assertRecordObjectInList(String msg, RecordObject desiredObject, 
			Collection objectList) {
		boolean found = false;
		Iterator iter = objectList.iterator();
		while ((found == false) && (iter.hasNext())) {
			Object obj = null;
		    try {
				obj = iter.next();
				RecordObject recordObj = (RecordObject)obj;
				if (recordObj.getId().equals(desiredObject.getId())) {
					found = true;
				}
		    } catch (ClassCastException ce) {
		        log.error("Class cast exception, class: " + obj.getClass().getName());
		        throw new SystemException("Class cast exception, class: " + obj.getClass().getName());
		    }
		}
		if (!found) {
			throw new AssertionFailedError(msg);
		}
	}
	
	
	public static void assertRecordObjectNotInList(RecordObject desiredObject, 
			Collection objectList) {
		assertRecordObjectNotInList("Assertion failed, record should not be in list", desiredObject, objectList);
	}
	
	public static void assertRecordObjectNotInList(String msg, RecordObject desiredObject, 
			Collection objectList) {
		boolean found = false;
		Iterator iter = objectList.iterator();
		while ((found == false) && (iter.hasNext())) {
			RecordObject obj = (RecordObject)iter.next();
			if (obj.getId().equals(desiredObject.getId())) {
				found = true;
			}
		}
		if (found) {
			throw new AssertionFailedError(msg);
		}
	}
	
	public static void assertCodedObjectNotInList(CodedObject desiredObject, 
			Collection objectList) {
		assertCodedObjectNotInList("Assertion failed, record should not be in list", desiredObject, objectList);
	}
	
	public static void assertCodedObjectNotInList(String msg, CodedObject desiredObject, 
			Collection objectList) {
		boolean found = false;
		Iterator iter = objectList.iterator();
		while ((found == false) && (iter.hasNext())) {
			CodedObject obj = (CodedObject)iter.next();
			if (obj.getCode().equals(desiredObject.getCode())) {
				found = true;
			}
		}
		if (found) {
			throw new AssertionFailedError(msg);
		}
	}
	
	public static void assertCodedObjectInList(CodedObject desiredObject, 
			Collection objectList) {
		assertCodedObjectInList("Assertion failed, record should be in list", desiredObject, objectList);
	}
	
	public static void assertCodedObjectInList(String msg, CodedObject desiredObject, 
			Collection objectList) {
		boolean found = false;
		Iterator iter = objectList.iterator();
		while ((found == false) && (iter.hasNext())) {
			CodedObject obj = (CodedObject)iter.next();
			if (obj.getCode().equals(desiredObject.getCode())) {
				found = true;
			}
		}
		if (!found) {
			throw new AssertionFailedError(msg);
		}
	}
	
	
	
	public static void assertRecordIdInList(Long desiredId, 
			Collection objectList) {
		assertRecordIdInList("Assertion failed, record should be in list", desiredId, objectList);
	}
	
	public static void assertRecordIdInList(String msg, Long desiredId, 
			Collection objectList) {
		boolean found = false;
		Iterator iter = objectList.iterator();
		while ((found == false) && (iter.hasNext())) {
			String obj = (String)iter.next();
			if (obj.equals(desiredId)) {
				found = true;
			}
		}
		if (!found) {
			throw new AssertionFailedError(msg + " / id = " + desiredId);
		}
	}
	
	
	public static void assertRecordIdNotInList(Long desiredId, 
			Collection objectList) {
		assertRecordIdNotInList("Assertion failed, record should not be in list", desiredId, objectList);
	}
	
	public static void assertRecordIdNotInList(String msg, Long desiredId, 
			Collection objectList) {
		boolean found = false;
		Iterator iter = objectList.iterator();
		while ((found == false) && (iter.hasNext())) {
			String obj = (String)iter.next();
			if (obj.equals(desiredId)) {
				found = true;
			}
		}
		if (found) {
			throw new AssertionFailedError(msg + " / id = " + desiredId);
		}
	}
	
}
